define(['module/ng-datatable/script' , 'module/md-org-device-tree/script'  , 'ztree'],function(){
	var controller = function( $scope , $timeout , $ctx , $ajax ){
		pwin.setLabel('监控配置') ;
		var pageContext = {

		};
		$scope.dimDataTable = {
			setting : {
				aoColumns : 	[{
					mData 	: 	'id' ,
					sTitle 	: 	'ID' ,
					sWidth 	: 	'5%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	true ,
					render 	: 	function( data , st , row ){
						return data || '--' ;
					}
				},{
					mData 	: 	'dimName' ,
					sTitle 	: 	'名称' ,
					sWidth 	: 	'8%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	false ,
					render 	: 	function( data , st , row ){
						return data || '--' ;
					}
				},{
					mData 	: 	'note' ,
					sTitle 	: 	'描述' ,
					sWidth 	: 	'5%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	false ,
					render 	: 	function( data , st , row ){
						return data || '--' ;
					}
				},{
					mData 	: 	'createTime' ,
					sTitle 	: 	'创建日期' ,
					sWidth 	: 	'5%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	false ,
					render 	: 	function( data , st , row ){
						return data && new Date( data * 1000 ).format('yyyy/MM/dd') || '--' ;
					}
				},{
					mData 	: 	'createName' ,
					sTitle 	: 	'创建人' ,
					sWidth 	: 	'8%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	false ,
					render 	: 	function( data , st , row ){
						return data || '--' ;
					}
				},{
					mData 	: 	'id' ,
					sTitle 	: 	'操作' ,
					sWidth 	: 	'5%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	true ,
					render 	: 	function( data , st , row ){
						var btnGroup = $('<div></div>').addClass('btn-group-inline') ;
						$('<a>修改</a>').attr({
							'class' 	: 	'btn btn-info btn-xs' ,
							'data-id' 	: 	data ,
							'name' : 	'update-btn'
						}).appendTo( btnGroup ) ;

						$('<a>删除</a>').attr({
							'class' 	: 	'btn btn-danger btn-xs' ,
							'data-id' 	: 	data ,
							'name' : 	'delete-btn'
						}).appendTo( btnGroup ) ;

						return btnGroup.children().length && btnGroup[0].outerHTML || '暂无操作' ;
					}
				}],
				ajaxSource : $ctx.cfgApi + 'device/1/dim'  
			},
			table 	: 	undefined
		}

		$scope.serviceDataTable = {
			setting : {
				aoColumns : 	[{
					mData 	: 	'id' ,
					sTitle 	: 	'ID' ,
					sWidth 	: 	'5%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	true ,
					render 	: 	function( data , st , row ){
						return data || '--' ;
					}
				},{
					mData 	: 	'serviceName' ,
					sTitle 	: 	'名称' ,
					sWidth 	: 	'8%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	false ,
					render 	: 	function( data , st , row ){
						return data || '--' ;
					}
				},{
					mData 	: 	'note' ,
					sTitle 	: 	'描述' ,
					sWidth 	: 	'5%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	false ,
					render 	: 	function( data , st , row ){
						return data || '--' ;
					}
				},{
					mData 	: 	'createTime' ,
					sTitle 	: 	'创建日期' ,
					sWidth 	: 	'5%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	false ,
					render 	: 	function( data , st , row ){
						return data && new Date( data * 1000 ).format('yyyy/MM/dd') || '--' ;
					}
				},{
					mData 	: 	'createName' ,
					sTitle 	: 	'创建人' ,
					sWidth 	: 	'8%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	false ,
					render 	: 	function( data , st , row ){
						return data || '--' ;
					}
				},{
					mData 	: 	'id' ,
					sTitle 	: 	'操作' ,
					sWidth 	: 	'5%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	true ,
					render 	: 	function( data , st , row ){
						var btnGroup = $('<div></div>').addClass('btn-group-inline') ;
						$('<a>取消关联</a>').attr({
							'class' 	: 	'btn btn-warning btn-xs glyphicon glyphicon-link' ,
							'unbind-device-dim-service-btn' : 	data
						}).appendTo( btnGroup ) ;
						return btnGroup.children().length && btnGroup[0].outerHTML || '暂无操作' ;
					}
				}],
				ajaxSource : $ctx.cfgApi + 'device/1/deviceService'  
			},
			table 	: 	undefined
		}
			
		$scope.sideTree = {
			setting 	: 	{
				callback 	: 	{
					onClick 	: 	function( event , treeId , node ){
						$timeout( function(){
							if( node['nodeType'] == 'device' ){
								$.extend( true , pageContext , node.extra ) ;

								$scope.dataListType = 'deviceDim' ;
								$scope.dimDataTable.setting['ajaxSource'] = $ctx.cfgApi + 'device/'+ pageContext['deviceId'] +'/dim' ;
							}else if( node['nodeType'] == 'deviceDim'){
								$.extend( true , pageContext , node.extra ) ;

								$scope.dataListType = 'serviceList' ;
								$scope.serviceDataTable.setting['ajaxSource'] = $ctx.cfgApi + 'device/'+pageContext['deviceId']+'/deviceService?dimId=' + pageContext['deviceDimId'] ;
							} else if( node['nodeType'] == 'service' ){
								$scope.dataListType = 'service' ;

							}
						});
					}
				}
			} ,
			tree 		: 	null
		}

		//------------------维度关联监控项-----------------------------
		$scope.dimAddServiceHandler = {
			setting : {
				aoColumns : 	[{
					mData 	: 	'id' ,
					sTitle 	: 	'ID' ,
					sWidth 	: 	'5%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	true ,
					render 	: 	function( data , st , row ){
						return data || '--' ;
					}
				},{
					mData 	: 	'serviceName' ,
					sTitle 	: 	'名称' ,
					sWidth 	: 	'8%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	false ,
					render 	: 	function( data , st , row ){
						return data || '--' ;
					}
				},{
					mData 	: 	'note' ,
					sTitle 	: 	'描述' ,
					sWidth 	: 	'5%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	false ,
					render 	: 	function( data , st , row ){
						return data || '--' ;
					}
				},{
					mData 	: 	'existsFlag' ,
					sTitle 	: 	'是否关联' ,
					sWidth 	: 	'5%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	false ,
					render 	: 	function( data , st , row ){
						return  data && '已关联' || '未关联';
					}
				},{
					mData 	: 	'id' ,
					sTitle 	: 	'操作' ,
					sWidth 	: 	'5%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	true ,
					render 	: 	function( data , st , row ){
						var btnGroup = $('<div></div>').addClass('btn-group-inline') ;
						if( !row.existsFlag )
							$('<a>关联</a>').attr({
								'class' 	: 	'btn btn-primary btn-xs glyphicon glyphicon-link' ,
								'bind-device-dim-service-btn' : 	data
							}).appendTo( btnGroup ) ;
						else
							$('<a>取消</a>').attr({
								'class' 	: 	'btn btn-warning btn-xs glyphicon glyphicon-link' ,
								'unbind-device-dim-service-btn' : 	data
							}).appendTo( btnGroup ) ;
						return btnGroup.children().length && btnGroup[0].outerHTML || '暂无操作' ;
					}
				}],
				aLengthMenu 		: 	[ [ 5 ] ] ,
				ajaxSource : null  
			},
			table 	: 	undefined ,
			init 	: 	function(){
				this.modal = '#dim-add-service-modal' ;
				this.tableList = '#dim-add-service-datatable-list' ;
				var _this = this ;
				$('#content-wapper').delegate('#service-create-btn' , 'click' , function(){
					location.href='#/config-manager/device/4/dim/1/createService' ;
				});
				$('#content-wapper').delegate( this.modal + ' [data-dim-service-filter]' , 'click' , function(){
					_this.loadServiceData( $(this).attr('data-dim-service-filter') ) ;
				} );
				$('#content-wapper').delegate('[bind-device-dim-service-btn]' , 'click' , function(){
					_this.bind( $(this).attr('bind-device-dim-service-btn') ) ;
				});
				$('#content-wapper').delegate('[unbind-device-dim-service-btn]' , 'click' , function(){
					_this.unbind( $(this).attr('unbind-device-dim-service-btn') ) ;
				});
				return this ;
			},
			reset 	: 	function(){

			},
			show 	: 	function(){
				this.loadServiceData('have') ;
				$(this.modal).modal('show') ;
			},
			loadServiceData 	: 	function( type ){
				type = type || 'have' ;
				var url = {
					have 	: 	$ctx.cfgApi + util.format('/device/$deviceId/deviceService?dimId=$deviceDimId' , pageContext ) ,
					all 	: 	$ctx.cfgApi + util.format('service?deviceGroupId=$deviceGroupId&deviceHas=$deviceId&dimHas=$deviceDimId' , pageContext ) 
				}

				var _this = this ;
				$timeout(function(){
					_this.setting['ajaxSource'] = url[type];	
				});
				if( $ctx.debug )
					console.log('关联监控项:' , this.table ) ;

				$(this.modal).find('[data-dim-service-filter]').find('i').removeClass('glyphicon-check').addClass('glyphicon-unchecked');
				$(this.modal).find('[data-dim-service-filter='+type+']').find('i').removeClass('glyphicon-unchecked').addClass('glyphicon-check');
			},
			unbind 	: 	function( id ){
				var _this = this , data = {
					deviceId 	: 	pageContext['deviceId'] ,
					dimId 		: 	pageContext['deviceDimId'] ,
					serviceId 	: 	id
				} ;

				if( $ctx.debug )
					console.log('取消关联监控项' , data ) ;
				
				$ajax.remove( $ctx.cfgApi + util.format(  'device/$deviceId/dim/$dimId/service/$serviceId' 
					, data )  ).success( function( result ){
					if( result.code != 0 )
						return alert( result.message ) ;
					_this.table.draw( false ) ;
					//刷新外层列表
					$scope.serviceDataTable.table.draw( false ) ;
				});
			},
			bind 	: 	function( id ){
				var data = {
					deviceId 	: 	pageContext['deviceId'] ,
					dimId 		: 	pageContext['deviceDimId'] ,
					serviceId 	: 	id
				} , jsonParam = JSON.stringify(data) , _this = this  ;

				if( $ctx.debug )
					console.log('添加关联监控项：' , data ) ;

				$ajax.post( $ctx.cfgApi + util.format('device/$deviceId/dim/$dimId/service/$serviceId' , data ) 
					).success( function( result ){
					if( result.code != 0 )
						return alert( result.message ) ;
					_this.table.draw( false ) ;
					//刷新外层列表
					$scope.serviceDataTable.table.draw( false ) ;
				});
			}
		}
		//------------------#维度关联监控项-----------------------------

		$scope.dimCreateForm = {
			fields 	: 	[[{
				label 	: 	'维度名称' ,
				name 	: 	'dimName' ,
				rule 	: 	{
					required 	: 	true ,
					maxlength 	: 	20 
				},
				help 	: 	'长度不大于20个字符' ,
				cols 	: 	12
			}],[{
				label 	: 	'描述' ,
				name 	: 	'note' ,
				rule 	: 	{
					required 	: 	false ,
					maxlength 	: 	200 
				},
				help 	: 	'长度不大于200个字符' ,
				cols 	: 	12
			}]],
			data 	: 	{}
		} ;

		var dimCreateHandler = {//新增			
			rule 	: 	{
				
			},
			init 	: 	function(){				
				this.modal = $('#dim-create-modal') ;
				this.form = $('#dim-create-form') ;
				var _this = this ;
				$('#content-wapper').delegate( '#dim-create-btn' , 'click' , function(){
					_this.show() ;
				});
				$(this.modal).delegate('[name=positive-btn]' ,'click' , function(){
					_this.submit($scope.dimCreateForm.data) ;
				});
				this.validator = $(this.form).validate( this.rule ) ;
				return this ;
			},
			reset 	: 	function(){
				var _this = this ;
				$timeout( function(){
					//重置样式
					$(_this.form).find('.has-error , .has-success').removeClass('has-success').removeClass('has-error') ;
					//重置数据
					$scope.dimCreateForm.data = {} ;
				});
			},
			show 	: 	function(){		
				if( !pageContext || !pageContext['deviceId'] )
					return alert('请选择一个设备!') ;

				this.reset() ;//重置表单
				$(this.modal).modal('show') ;
			},
			submit 	: 	function( data ){
				var _this = this ;
				if( !this.validator.form() )
					return false ;
				console.log( '添加数据' , data , JSON.stringify( data ) ) ;
				//-------------------------配置新增接口地址--------------------------------
				$ajax.post($ctx.cfgApi+'device/'+pageContext['deviceId']+'/dim', JSON.stringify( data ) ).success( function( result ){
					console.log('添加成功' , result ) ;
					if( result.code != 0 )
						return alert( result.message ) ;

					$(_this.modal).modal('hide') ;
					alert('添加成功!' ) ;
					$scope.dimDataTable.table.draw( true ) ;
				});
			}
		}


		$scope.dimUpdateForm = {
			fields 	: 	[[{
				label 	: 	'维度名称' ,
				name 	: 	'dimName' ,
				rule 	: 	{
					required 	: 	true ,
					maxlength 	: 	20 
				},
				help 	: 	'长度不大于20个字符' ,
				cols 	: 	12
			}],[{
				label 	: 	'描述' ,
				name 	: 	'note' ,
				rule 	: 	{
					required 	: 	false ,
					maxlength 	: 	200 
				},
				help 	: 	'长度不大于200个字符' ,
				cols 	: 	12
			}]],
			data 	: 	{}
		} ;

		var dimUpdateHandler = {//修改
			rule 	: 	{

			},
			init: function() {	
				this.modal = $('#dim-update-modal') ;
				this.form = $('#dim-update-form') ;
				var _this = this ;
				$('#content-wapper').delegate('#dim-datatable-list [name=update-btn]', 'click', function() {
					if( !pageContext || !pageContext['deviceId'] )
						return alert('请选择设备!') ;

					_this.show( $(this).attr("data-id") ); 
				});	
				$(this.modal).delegate('[name=positive-btn]' ,'click' , function(){
					_this.submit( $scope.dimUpdateForm.data ) ;
				});
				this.validator = $(this.form).validate( this.rule ) ;
				return this
			},
			loadDetail 	: 	function( id , then ){
				//-------------------------配置详情接口地址--------------------------------
				$ajax.get( $ctx.cfgApi + 'dim/' + id ).success( function( result ){
					if( result.code != 0 )
						return alert( result.message ) ;

					var data = result.data ;
					if( !data )
						return alert('数据不存在!' ) ; 

					$timeout( function(){
						$scope.dimUpdateForm.data = data;
					});
					then( result.data ) ;
				});
			},
			reset 	: 	function(){
				var _this = this ;
				$timeout( function(){
					//重置样式
					$(_this.form).find('.has-error , .has-success').removeClass('has-success').removeClass('has-error') ;
					//重置数据
					$scope.updateForm.data = {} ;
				});
			},
			show 		: 	function( id ){
				if( typeof id == 'undefined' )
					return alert('请选择一条数据!') ;
				this.id = id ;

				this.reset() ;//重置表单

				var _this = this ;
				this.loadDetail( this.id , function( ){
					$(_this.modal).modal('show') ;
				});
			},
			submit 	: 	function( data ){
				if( !this.validator.form() )
					return false ;

				var _this = this , param = util.editableData( $scope.dimUpdateForm.fields , data ) ;

				//-------------------------配置修改接口地址--------------------------------
				$ajax.put( $ctx.cfgApi+ 'dim/' + this.id ,JSON.stringify( param ) ).success( function( result ){
					if( result.code != 0 )
						return alert( result.message ) ;

					$(_this.modal).modal('hide') ;
					alert('更新成功!' ) ;
					$scope.dimDataTable.table.draw( false ) ;
				});
			}
		}

		

		/*$scope.createForm = {
			fields 	: 	[[{
				label 	: 	'角色名称' ,
				name 	: 	'name' ,
				rule 	: 	{
					required 	: 	true ,
					maxlength 	: 	20 
				},
				help 	: 	'长度不大于20个字符' ,
				cols 	: 	6
			},{
				label 	: 	'角色标识' ,
				name 	: 	'code' ,
				rule 	: 	{
					required 	: 	true ,
					maxlength 	: 	20 
				},
				help 	: 	'长度不大于20个字符' ,
				cols	: 	6
			}],[{
				label 	: 	'描述' ,
				name 	: 	'note' ,
				rule 	: 	{
					required 	: 	false ,
					maxlength 	: 	200 
				},
				help 	: 	'长度不大于200个字符' ,
				cols 	: 	12
			}]],
			data 	: 	{}
		} ;

		var createHandler = {//新增			
			rule 	: 	{
				
			},
			init 	: 	function(){				
				this.modal = $('#create-modal') ;
				this.form = $('#create-form') ;
				var _this = this ;
				$('#create-btn').bind('click' , function(){
					_this.show() ;
				});
				$(this.modal).delegate('[name=positive-btn]' ,'click' , function(){
					_this.submit($scope.createForm.data) ;
				});
				this.validator = $(this.form).validate( this.rule ) ;
				return this ;
			},
			reset 	: 	function(){
				var _this = this ;
				$timeout( function(){
					//重置样式
					$(_this.form).find('.has-error , .has-success').removeClass('has-success').removeClass('has-error') ;
					//重置数据
					$scope.createForm.data = {} ;
				});
			},
			show 	: 	function(){				
				this.reset() ;//重置表单
				$(this.modal).modal('show') ;
			},
			submit 	: 	function( data ){
				var _this = this ;
				if( !this.validator.form() )
					return false ;
				console.log( '添加数据' , data , JSON.stringify( data ) ) ;
				//-------------------------配置新增接口地址--------------------------------
				$ajax.post($ctx.api+'role', JSON.stringify( data ) ).success( function( result ){
					console.log('添加成功' , result ) ;
					if( result.code != 0 )
						return alert( result.message ) ;

					$(_this.modal).modal('hide') ;
					alert('添加成功!' ) ;
					$scope.dataTable.table.draw( true ) ;
				});
			}
		}
*/
		
		/*$scope.updateForm = {
			fields 	: 	[[{
				label 	: 	'角色名称' ,
				name 	: 	'name' ,
				rule 	: 	{
					required 	: 	true ,
					maxlength 	: 	20 
				},
				help 	: 	'长度不大于20个字符' ,
				cols 	: 	6
			},{
				label 	: 	'角色标识' ,
				name 	: 	'code' ,
				rule 	: 	{
					required 	: 	true ,
					maxlength 	: 	20 
				},
				help 	: 	'长度不大于20个字符' ,
				cols	: 	6
			}],[{
				label 	: 	'描述' ,
				name 	: 	'note' ,
				rule 	: 	{
					required 	: 	false ,
					maxlength 	: 	200 
				},
				help 	: 	'长度不大于200个字符' ,
				cols 	: 	12
			}]],
			data 	: 	{}
		} ;
		var updateHandler = {//修改
			rule 	: 	{

			},
			init: function() {	
				this.modal = $('#update-modal') ;
				this.form = $('#update-form') ;
				var _this = this ;
				$('#datatable-list').delegate('[name=update-btn]', 'click', function() {			
					_this.show( $(this).attr("data-id") ); 
				});	
				$(this.modal).delegate('[name=positive-btn]' ,'click' , function(){
					_this.submit( $scope.updateForm.data ) ;
				});
				this.validator = $(this.form).validate( this.rule ) ;
				return this
			},
			loadDetail 	: 	function( id , then ){
				//-------------------------配置详情接口地址--------------------------------
				$ajax.get( $ctx.api + 'role/' + id ).success( function( result ){
					if( result.code != 0 )
						return alert( result.message ) ;

					var data = result.data ;
					if( !data )
						return alert('数据不存在!' ) ; 

					$timeout( function(){
						$scope.updateForm.data = data;
					});
					then( result.data ) ;
				});
			},
			reset 	: 	function(){
				var _this = this ;
				$timeout( function(){
					//重置样式
					$(_this.form).find('.has-error , .has-success').removeClass('has-success').removeClass('has-error') ;
					//重置数据
					$scope.updateForm.data = {} ;
				});
			},
			show 		: 	function( id ){
				if( typeof id == 'undefined' )
					return alert('请选择一条数据!') ;
				this.id = id ;

				this.reset() ;//重置表单

				var _this = this ;
				this.loadDetail( this.id , function( ){
					$(_this.modal).modal('show') ;
				});
			},
			submit 	: 	function( data ){
				if( !this.validator.form() )
					return false ;

				var _this = this , param = util.editableData( $scope.updateForm.fields , data ) ;

				//-------------------------配置修改接口地址--------------------------------
				$ajax.put( $ctx.api+'role/' + this.id ,JSON.stringify( param ) ).success( function( result ){
					if( result.code != 0 )
						return alert( result.message ) ;

					$(_this.modal).modal('hide') ;
					alert('更新成功!' ) ;
					$scope.dataTable.table.draw( false ) ;
				});
			}
		}*/

		$scope.deviceServiceAlertForm = {
			fields 	: 	[[{
				label 	: 	'严重条件' ,
				name 	: 	'value' ,
				rule 	: 	{
					required 	: 	true ,
					maxlength 	: 	8 
				},
				cols	: 	6
			},{
				label 	: 	'阈值' ,
				name 	: 	'value' ,
				rule 	: 	{
					required 	: 	true ,
					maxlength 	: 	8 
				},
				cols	: 	6
			}],[{
				label 	: 	'警告条件' ,
				name 	: 	'express' ,
				rule 	: 	{
					required 	: 	true ,
					maxlength 	: 	20 
				},
				cols 	: 	6
			},{
				label 	: 	'阈值' ,
				name 	: 	'value' ,
				rule 	: 	{
					required 	: 	true ,
					maxlength 	: 	8 
				},
				cols	: 	6
			}]],
			data 	: 	{}
		} ;

		$scope.deviceServiceParamForm = {
			fields 	: 	[[{
				label 	: 	'IP地址' ,
				name 	: 	'ip' ,
				rule 	: 	{
					required 	: 	true ,
					maxlength 	: 	20 
				},
				cols 	: 	6
			},{
				label 	: 	'端口号' ,
				name 	: 	'port' ,
				rule 	: 	{
					required 	: 	true ,
					maxlength 	: 	8 
				},
				cols	: 	6
			}],[{
				label 	: 	'团体名' ,
				name 	: 	'publicName' ,
				rule 	: 	{
					required 	: 	true ,
					maxlength 	: 	50 
				},
				cols 	: 	6
			}]],
			data 	: 	{}
		} ;

		var deviceServiceUpdateHandler = {//新增			
			rule 	: 	{
				
			},
			init 	: 	function(){
				this.form = $('#device-service-form') ;
				var _this = this ;
				this.validator = $(this.form).validate( this.rule ) ;
				return this ;
			},
			reset 	: 	function(){
				var _this = this ;
				this.loadDetail( function( data ){
					//重置样式
					$(_this.form).find('.has-error , .has-success').removeClass('has-success').removeClass('has-error') ;
					//清空数据
					$scope.deviceServiceParamForm.data = {} ;

					//有实例参数初始化 .
					if( data['hasInstance'] > 0 ){
						_this.resetInstanceForm( data['deviceInstances'] ) ;
					}else{ //无实例参数初始化 .
						_this.resetAlertForm( data ) ;
						_this.resetParamForm( data['params'] ) ;
					}
					
					$timeout();
				});
				return this ;
			},
			resetAlertForm 	: 	function( data ){

			},
			resetInstanceForm : function( data ){
				this.resetAlertForm( data ) ;
				this.resetParamForm( data['params'] ) ;
			},
			resetParamForm 	: 	function( data ){

			},
			loadDetail 	: 	function(  then ){
				$ajax.get( $ctx.cfgApi + util.format('device/$deviceId/service/$serviceId' , pageContext) )
					.success( function( result ){
						if( result.code != 0 )
							return alert( result.message ) ;
						if( !result.data )
							return alert( '数据不存在!' ) ;
						then( result.data ) ;
					});
			},
			show 	: 	function(){				
				this.reset() ;//重置表单
			},
			submit 	: 	function( data ){
				/*var _this = this ;
				if( !this.validator.form() )
					return false ;
				console.log( '添加数据' , data , JSON.stringify( data ) ) ;
				//-------------------------配置新增接口地址--------------------------------
				$ajax.post($ctx.api+'role', JSON.stringify( data ) ).success( function( result ){
					console.log('添加成功' , result ) ;
					if( result.code != 0 )
						return alert( result.message ) ;

					$(_this.modal).modal('hide') ;
					alert('添加成功!' ) ;
					$scope.dataTable.table.draw( true ) ;
				});*/
			}
		}

		var dimDeleteHandler = {
			init 	: 	function(){
				var _this = this ;
				$('#content-wapper').delegate('#dim-datatable-list [name=delete-btn]', 'click', function() {	
					var _ele = this ;		
					confirm('是否删除?' , function(){
						_this.del( $(_ele).attr("data-id") ) ;
					});
				});
				return this ;
			},
			del 	: 	function( id ){
				//-------------------------配置删除接口地址--------------------------------
				$ajax.remove( $ctx.cfgApi + 'dim/' + id ).success( function( result ){
					if( result.code != 0 )
						return alert( result.message ) ;
					alert('删除成功!') ;
					$scope.dimDataTable.table.draw( false ) ;
				});
			}
		}

		
		//页面初始化入口
		$timeout( function(){
			dimCreateHandler.init() ;
			dimUpdateHandler.init() ;
			dimDeleteHandler.init() ;

			$scope.dimAddServiceHandler.init() ;
			deviceServiceUpdateHandler.init() ;
		});
	}
	return controller ;
});